/**
 * 
 */
package vn.support.connection.response.processor.impl;

import java.util.Collection;

import org.apache.log4j.Logger;

import vn.support.connection.Connection;
import vn.support.connection.response.processor.AbstractResponseProcessor;
import vn.support.core.dto.AppInfo;
import vn.support.core.dto.ChatExec;
import vn.support.core.dto.DataObject;
import vn.support.core.dto.DataObjectUtil;
import vn.support.core.dto.PendingChat;
import vn.support.core.dto.Staff;
import vn.support.core.dto.Visitor;


/**
 * @author nttuyen
 *
 */
public class FetchVisitorProcessor extends AbstractResponseProcessor {
	private static final Logger log = Logger.getLogger(FetchVisitorProcessor.class);
	
	public FetchVisitorProcessor() {
		super();
	}
	
	public DataObject process(DataObject data) {
		String xml = data.get(Connection.DTO_RESPONSE_DATA);
		if(!xml.startsWith("<?xml")) {
			log.debug("Response is not xml!!!");
			return data;
		}
		
		DataObject systemData = DataObjectUtil.get(DataObject.SYSTEM_DATA_OBJECT_NAME);
		Collection<Visitor> visitors = this.entityFactory.create(xml, "//swiftwinapp/visitor", Visitor.class);
		systemData.set(Visitor.class.getName(), visitors);
		
		Collection<Staff> staffs = this.entityFactory.create(xml, "//swiftwinapp/staff", Staff.class);
		systemData.set(Staff.class.getName(), staffs);
		
		Collection<PendingChat> pendingChats = this.entityFactory.create(xml, "//swiftwinapp/pendingchat", PendingChat.class);
		systemData.set(PendingChat.class.getName(), pendingChats);
		
		Collection<ChatExec> chatExecs = this.entityFactory.create(xml, "//swiftwinapp/chatexec", ChatExec.class);
		systemData.set(ChatExec.class.getName(), chatExecs);
		
		Collection<AppInfo> appInfos = this.entityFactory.create(xml, "//swiftwinapp/info", AppInfo.class);
		
		//Save Data to SystemData
		DataObject system = DataObjectUtil.get(DataObject.SYSTEM_DATA_OBJECT_NAME);
		system.set(Visitor.class.getName(), visitors);
		system.set(Staff.class.getName(), staffs);
		system.set(PendingChat.class.getName(), pendingChats);
		system.set(ChatExec.class.getName(), chatExecs);
		system.set(AppInfo.class.getName(), appInfos);
		DataObjectUtil.set(DataObject.SYSTEM_DATA_OBJECT_NAME, system);
		
		return data;
	}
}
